package com.foreveross.crawl.complicating;


/**
 * 渠道并发数据管理器，管理组和常规任务
 * @author Administrator
 *
 */
public class ChannelComplicatingDataManager {/*
	private static ChannelComplicatingDataManager manager=null;
	//private static Map<GradChannelEntity,Counter> channelCounterData=null;
	private static Map<GradChannelEntity,Integer> channelMaxComplication=null;
	private static Map<GradChannelEntity,Integer> channelTimeout=null;//渠道超时时间，目前这个在常规任务有效
	private static double baseTaskComplitingProportionInAll=1;//常规任务渠道并发量占渠道总并发量的最高比率,默认100%
	private static Logger logger = Logger.getLogger(ChannelComplicatingDataManager.class);
	private ChannelComplicatingDataManager() {
		super();
	}
	*//**
	 * 初始化并发管理器
	 * @param channels 渠道
	 * @param isMaxSizeAuto 是否动态并发量
	 * @param defaultSize 如果不动态的默认值
	 * @param isTimeoutAuto 是否在组任务中动态超时时间
	 * @param defaultTimeout 如果组任务中不动态的默认超时时间
     * @param baseTaskComProportion 常规任务渠道并发量占渠道总并发量的最高比率
	 * @throws Exception
	 *//*
	public static void init(List<GradChannelEntity> channels,boolean isMaxSizeAuto,int defaultSize,
			boolean isTimeoutAuto,int defaultTimeout,double baseTaskComProportion) throws Exception{
		if(manager!=null){
			throw new Exception("ChannelComplicatingDataManager  Has Init!");
		}
		if(channels==null || channels.isEmpty()){
			throw new Exception("GradChannelEntity List Is Empty!");
		}
		manager=new ChannelComplicatingDataManager();
		channelMaxComplication=new HashMap<GradChannelEntity,Integer>();
		channelTimeout=new HashMap<GradChannelEntity,Integer>();
		defaultSize=defaultSize<0?0:defaultSize;
		for(GradChannelEntity channel:channels){
			if(channel==null){
				throw new Exception("Channel Is Null!");
			}
			if(!NodeInfoManager.getInstance().isChannelDoTaskByNodeType(channel.getId())){
				continue;
			}
			channelMaxComplication.put(channel, isMaxSizeAuto?channel.getBatchSize():defaultSize);
			channelTimeout.put(channel, isTimeoutAuto?channel.getSoTimeout():defaultTimeout);
			logger.info(String.format("初始化渠道 [%s] 最大抓取并发量 [%s] 常规任务超时时间[%s]", 
					channel.getName(),channelMaxComplication.get(channel),channelTimeout.get(channel)));
		}
		baseTaskComplitingProportionInAll=baseTaskComProportion;
		logger.info(String.format("渠道并发管理器 初始完成！ 是否允许动态并发量？[%s] 是否允许动态超时时间？[%s] 常规任务渠道并发量最高比例 [%s]", 
				isMaxSizeAuto,isTimeoutAuto,baseTaskComplitingProportionInAll));
	}

	*//**
	 * 获得渠道的最大并发量
	 * @param channel
	 * @return
	 *//*
	public int getChannelMaxComplicatingSize(GradChannelEntity channel){
		if(!channelMaxComplication.containsKey(channel)){return 0;}
		return channelMaxComplication.get(channel);
	}

	
	
	*//**
	 * 某个渠道是否到达最大并发量
	 * @param channel
	 * @return
	 * @throws Exception 
	 * @throws Exception 
	 *//*
	public boolean isReachComplicatingSize(GradChannelEntity channel,Scheduler scheduler ) throws Exception{
		return hasFreeComplicatingSizeInAll(channel,scheduler)<=0;
	}
	*//**
	 * 渠道全局剩余并发量
	 * @param channel
	 * @param scheduler
	 * @return
	 *//*
	public int hasFreeComplicatingSizeInAll(GradChannelEntity channel,Scheduler scheduler){
		if(!channelMaxComplication.containsKey(channel)){return 0;}
		try {
			//最大-组任务并发-常规任务并发
			int len=channelMaxComplication.get(channel)-
					scheduler.getJobNames( CrawlContext.CRAWL_TASK_CHANNEL_GROUP+channel.getId()).length-
					scheduler.getJobNames(CrawlContext.CRAWL_TASK_CHANNEL_BASE+channel.getId()).length;
			len=len<0?0:len;
			logger.info(String.format("渠道[%s] 剩余全局并发量[%s]", channel.getName(),len));
			return len;
		} catch (SchedulerException e) {}
		return 0;
	}

	*//**
	 * 获得渠道常规任务超时时间
	 * @param channel
	 * @return
	 *//*
	public int getChannelBaseTaskTimeout(GradChannelEntity channel){
		if(!channelTimeout.containsKey(channel)){return 0;}
		return channelTimeout.get(channel);
	}
	*//**
	 * 渠道常规任务的剩余并发量
	 * @param channel
	 * @param scheduler
	 * @return
	 *//*
	public int hasFreeComplicatingSizeInBase(GradChannelEntity channel,Scheduler scheduler){
		//常规最大-常规并发
		//常规最大
		int max=(int)(baseTaskComplitingProportionInAll*getChannelMaxComplicatingSize(channel));
		try {
			int base = scheduler.getJobNames(CrawlContext.CRAWL_TASK_CHANNEL_BASE+channel.getId()).length;
			if(base>=max){return 0;}
			int free=channelMaxComplication.get(channel)-
					scheduler.getJobNames( CrawlContext.CRAWL_TASK_CHANNEL_GROUP+channel.getId()).length;
			return Math.min(max-base, free);
		} catch (SchedulerException e1) {}
		return 0;
	}
	*//**
	 * 获得常规任务占渠道总并发的比率
	 * @return
	 *//*
	public double  getBaseTaskComplitingProportionInAll(){
		return baseTaskComplitingProportionInAll;
	}
	
	
	public static ChannelComplicatingDataManager getInstance(){
		return manager;
	}
	
*/}
